package com.kr.start.service.Demo;

import java.util.HashMap;
import java.util.Map;

import com.kr.start.bean.Demo.test1;
import com.kr.start.bean.Demo.test1_bak;
import com.kr.start.bean.Demo.test1_view;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.retry.annotation.Recover;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.retry.annotation.Backoff;

@Service
public class ttService {

    @Autowired
    private test1Service _test1Service;
    @Autowired
    private test1BakService _test1BakService;
    @Autowired
    DataSourceTransactionManager dataSourceTransactionManager;
    @Autowired
    TransactionDefinition transactionDefinition;

    /**
     * value: 那些异常导致重试 maxAttempts： 最大重试次数 delay:重试延迟时间 multiplier：上次延迟对于当前次的倍数
     * 
     * @throws Exception
     */
    @Retryable(value = Exception.class, maxAttempts = 3, backoff = @Backoff(delay = 1000, multiplier = 1.5))
    public Map<String, Integer> addtt(int value) throws Exception {
        Map<String,Integer> _out = new HashMap<String,Integer>(3);
        TransactionStatus transactionStatus = dataSourceTransactionManager.getTransaction(transactionDefinition);
        // try {
            test1_view tv1 = _test1Service.info(1);
            test1 t1 = new test1();
            t1.setId(tv1.getId());
            t1.setVersion_id(tv1.getVersion_id());
            t1.setValue(value);
            Integer _val = _test1Service.update(t1);
            test1_bak tb1 = new test1_bak();
            tb1.setVersion_id(t1.getVersion_id());
            tb1.setValue(value);
            Integer _val1 = _test1BakService.add(tb1);
            _out.put("versionid", t1.getVersion_id());
            _out.put("rollbackid", tb1.getId());
            if(_val>0 && _val1>0){
                dataSourceTransactionManager.commit(transactionStatus);//提交
                _out.put("val", _val);
                return _out;
            }else {
                dataSourceTransactionManager.rollback(transactionStatus); //回滚
                System.out.println(String.format("rollback,id:%s,version_id:%s", tv1.getId(),tv1.getVersion_id()));
                throw new Exception("rollback");
            }
        // } catch (Exception e) {
        //     dataSourceTransactionManager.rollback(transactionStatus); //回滚
        //     _out.put("val", 0);
        //     return _out;
        // }
        
    }

    @Recover
    public Map<String, Integer> recover(Exception e){
        System.out.println(String.format("recoverMessage:%s",e.getMessage()));
        //记日志到数据库 或者调用其余的方法
        Map<String,Integer> out = new HashMap<String,Integer>(3);
        out.put("versionid", 0);
        out.put("rollbackid", 0);
        out.put("val", 0);
        return out;
    }


}